package com.yztc.yidoutang.fragment.jingxuan_fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.yztc.yidoutang.R;
import com.yztc.yidoutang.activity.daren_activity.DaRenActivity;
import com.yztc.yidoutang.activity.quanwu_activity.QuanWuActivity;
import com.yztc.yidoutang.adapter.JingXuanListAdapter;
import com.yztc.yidoutang.base.BaseFragment;
import com.yztc.yidoutang.bean.JingXuanListBean;

import java.util.List;

/**
 * Created by My on 2016/10/17.
 */

public class JingXuanFragment extends BaseFragment implements JingXuanContract.View {
    private SwipeRefreshLayout swipeRefreshLayout;
    private RelativeLayout rl_quanwu,rl_daren;
    private ListView listView;
    private JingXuanListAdapter adapter;
    private JingXuanPresenter presenter=new JingXuanPresenter(this);
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        listView= (ListView) view.findViewById(R.id.jx_lv);
        listView.addHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.item_jingxuanhead,null));
        listView.addFooterView(LayoutInflater.from(getActivity()).inflate(R.layout.item_jingxuanfoot,null));
        rl_quanwu= (RelativeLayout) view.findViewById(R.id.jx_rl_quanwuji);
        rl_daren= (RelativeLayout) view.findViewById(R.id.jx_rl_darenshuo);
        swipeRefreshLayout= (SwipeRefreshLayout) view.findViewById(R.id.jx_swipe_layout);
        presenter.getDataForNet();
        refresh();
        click();
    }
    @Override
    public int getLayoutId() {
        return R.layout.fragment_jingxuan;
    }

    private void refresh() {
        //设置刷新时动画的颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.clr_orange);
        //表示刷新时出发的监听事件
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        presenter.getDataForNet();
                        swipeRefreshLayout.setRefreshing(false);
                    }
                },2000);

            }
        });
    }

    private void click() {
        rl_quanwu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), QuanWuActivity.class);
                startActivity(intent);
            }
        });
        rl_daren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(getActivity(), DaRenActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onJingXuanSuccess(List<JingXuanListBean> jingXuanBeanList) {
        adapter=new JingXuanListAdapter(getActivity(),jingXuanBeanList);
        listView.setAdapter(adapter);
    }

    @Override
    public void onJingXuanFail(String msg) {
        Toast.makeText(getActivity(),"获取数据失败",Toast.LENGTH_SHORT).show();
    }
}
